// 用来造home页面的数据的
// 这个页面是具体的执行的语句，以及伪造数据，相当于把php拆成两个部分来写，API中的就相当于php的前端部分，用于传输数据，
//这部分就相当于具体的MySQL的执行语句

var list=[];
import Mock from 'mockjs'
import { param2Obj } from "../untial/urltips";

for(var i = 0;i<100;i++){
    list.push({
         id:Mock.mock('@increment()'),
        //  id:Mock.mock('@increment()'),
        date:Mock.mock('@datetime()'),
        name:Mock.mock('@cname()'),
        address:Mock.mock('@county(true)'),
        money:Mock.mock('@natural(2000, 5000)'),
        count:Mock.mock('@natural(1, 10)'),
        type:Mock.mock('@natural(1, 3)')
    })
}

export default{
    gethomedata:config=>{
      //  console.log('config',config);
      var { keyword,page,pageSize } = param2Obj(config.url)
      var res =  list.filter(item=>item.name.indexOf(keyword) != -1)
      var resLimit = res.filter((item,index)=>{
        return ( page-1 ) * pageSize <= index && index < page * pageSize
    })
       return{
        code:200,
        message:"请求成功",
        data:resLimit  , 
        total:list.length
       }
    },

    deletehomedata:config=>{
        // console.log('config',config);
        var { id } =  JSON.parse(config.body)
        var res = list.filter(item=>item.id != id)
        // console.log(res);
        list=res
      return{
        code:200,
        message:"删除成功",
        data:list,
        total:list.length
      }
    },

    deletemoredata:config=>{
      var { ids } =  JSON.parse(config.body)
      var res = list.filter(item=>!ids.includes(item.id))
      list=res
    return{
      code:200,
      message:"删除多条数据成功",
      data:list,
      total:list.length
    }
  },


  addonedata:config=>{
    // console.log(JSON.parse(config.body));
    const {  name,address, type,count,money } = JSON.parse(config.body)
    list.unshift({
      id:Mock.mock('@increment()'),
      name:name,
      address:address,
      type:type,
      count:count,
      date:Mock.mock('@datetime()'),
      money:money
  })
  console.log(list);
  return{
    code:200,
    message:"添加数据成功",
    data:null 
  }
},

editonedata :config=>{
  // console.log(JSON.parse(config.body));
  const { id, name,address, type,count,money } = JSON.parse(config.body)
 list.forEach(item=>{
  if(item.id==id){
    item.name=name,
    item.address=address,
    item.type=type,
    item.count=count,
    item.money=money
    return
  }
 })
return{
  code:200,
  message:"修改成功",
  data:null 
}
},
}


